package cn.lee.share.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetIP {
    private GetIP(){
        
    }
    public static String  wifiIP(){
        Enumeration netInterfaces;
        try {
            netInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface nif = (NetworkInterface) netInterfaces.nextElement();
                Enumeration iparray = nif.getInetAddresses();
                while (iparray.hasMoreElements()) {
                    String ip =  ((InetAddress) iparray.nextElement()).getHostAddress();
                    if(ip.startsWith("192")){
                        return ip;
                    }
                }
            }
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "127.0.0.1";
    }
public static void main(String[] args) {
    try {
        System.out.println("ip: " +wifiIP());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
}
